package com.zyq.thymeleaf.controller;

import com.zyq.thymeleaf.bean.Admin;
import com.zyq.thymeleaf.bean.User;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Zyq
 * @version 1.0
 * @date 2024/12/31 周二
 */
@Slf4j
@Controller
public class AdminController {

    private static final String UPLOADED_FOLDER = "src/main/resources/static/uploads/";

    @PostMapping("/login")
    public String login(Admin admin, HttpSession session, Model model, @RequestParam("file") MultipartFile file) {
        if (StringUtils.hasText(admin.getUser()) && "666".equals(admin.getPassword())) {
            // 验证通过，将管理员信息保存到 session
            session.setAttribute("admin", admin);

            if (file.isEmpty()) {
                model.addAttribute("message", "文件是空的");
                return "login";
            } else {
                try {
                    // 确保上传目录存在
                    Path uploadPath = Paths.get(UPLOADED_FOLDER);
                    if (!Files.exists(uploadPath)) {
                        Files.createDirectories(uploadPath);
                    }

                    // 保存文件
                    byte[] bytes = file.getBytes();
                    Path path = uploadPath.resolve(file.getOriginalFilename());
                    Files.write(path, bytes);

                    model.addAttribute("message", "文件上传成功：" + file.getOriginalFilename());
                } catch (IOException e) {
                    model.addAttribute("message", "文件上传失败：" + e.getMessage());
                    return "login";
                }
            }

            // 验证通过，重定向到管理页面
            return "redirect:/admin.html";
        } else {
            // 验证失败，返回登录页面
            model.addAttribute("message", "用户名或密码不正确");
            return "login";
        }
    }


    @GetMapping("/admin.html")
    public String admin(Model model, HttpSession session) {

        Object admin = session.getAttribute("admin");

        //  if (admin != null) {
        //log.info("进入mainpage");


        List<User> users = new ArrayList<>();
        users.add(new User(1, "关羽", "333", 10, "gy@sohu.com"));
        users.add(new User(1, "关羽", "password1", 35, "gy@sohu.com"));
        users.add(new User(2, "张飞", "password2", 33, "zf@sohu.com"));
        users.add(new User(3, "刘备", "password3", 40, "lb@sohu.com"));
        users.add(new User(4, "赵云", "password4", 29, "zy@sohu.com"));
        users.add(new User(5, "马超", "password5", 27, "mc@sohu.com"));
        users.add(new User(6, "黄忠", "password6", 55, "hz@sohu.com"));
        users.add(new User(7, "诸葛亮", "password7", 38, "zgl@sohu.com"));
        users.add(new User(8, "庞统", "password8", 31, "pt@sohu.com"));
        users.add(new User(9, "孙尚香", "password9", 25, "ssx@sohu.com"));
        users.add(new User(10, "周瑜", "password10", 28, "zy@sohu.com"));


        model.addAttribute("users", users);


        return "admin";
        //  }
        //   model.addAttribute("message", "不合法");
        //    return "login";
    }
}
